package tclp.gui.panels;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import net.sourceforge.javaocr.gui.meanSquareOCR.TrainingImageSelector;
import net.sourceforge.javaocr.gui.meanSquareOCR.TrainingImageSpec;
import net.sourceforge.javaocr.ocrPlugins.mseOCR.TrainingImage;
import tclp.gui.GUIController;
import tclp.simpleocr.TrainingImageUtils;

/**
 * 
 * @author Absatz Leoš
 */
public class SettingsTrainPanel extends JPanel {

  private GUIController controller;

  private TrainingImageSelector trainingImageSelector;

  private static Dimension btnSize = new Dimension(150, 25);
  
  private HashMap<Character, ArrayList<TrainingImage>> trainingImages;

  public SettingsTrainPanel(GUIController controller) {
    this.controller = controller;
    this.trainingImageSelector = new TrainingImageSelector();
    this.trainingImages = controller.getTrainingImages();
    initComponents();
  }

  private void initComponents() {
    this.setLayout(new BorderLayout());

    JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    this.add(panel, BorderLayout.NORTH);

    GridBagConstraints c = new GridBagConstraints();

    JLabel label = new JLabel("Settings Train");
    c.fill = GridBagConstraints.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(10, 10, 10, 10);
    c.gridwidth = 2;
    c.anchor = GridBagConstraints.NORTHWEST;
    c.weightx = 1;
    panel.add(label, c);

    c = new GridBagConstraints();
    c.weightx = 1;
    c.weighty = 1;
    c.gridx = 0;
    c.gridy = 1;
    c.anchor = GridBagConstraints.WEST;
    c.fill = GridBagConstraints.BOTH;
    panel.add(trainingImageSelector, c);
    
    JPanel btnPanel = new JPanel();
    btnPanel.setLayout(new GridBagLayout());

    //Create buttons
    JButton addBtn = new JButton("Add Training Image");
    addBtn.addActionListener(getAddBtnListener());

    JButton delBtn = new JButton("Delete Training Image");
    delBtn.addActionListener(getDelBtnListener());

    JButton processBtn = new JButton("Create Training Set");
    processBtn.addActionListener(getProcessBtnListener());

    //Set button size
    delBtn.setMinimumSize(btnSize);
    delBtn.setPreferredSize(btnSize);
    delBtn.setMaximumSize(btnSize);

    addBtn.setMinimumSize(btnSize);
    addBtn.setPreferredSize(btnSize);
    addBtn.setMaximumSize(btnSize);

    processBtn.setMinimumSize(btnSize);
    processBtn.setPreferredSize(btnSize);
    processBtn.setMaximumSize(btnSize);

    //Add to panel
    c = new GridBagConstraints();
    c.gridx = 0;
    c.gridy = 1;
    c.anchor = GridBagConstraints.NORTHWEST;
    btnPanel.add(addBtn, c);
    c.gridy = 2;
    btnPanel.add(delBtn, c);
    c.gridy = 3;
    c.insets = new Insets(20, 0, 0, 0);
    btnPanel.add(processBtn, c);

    c = new GridBagConstraints();
    c.gridx = 1;
    c.gridy = 1;
    c.insets = new Insets(0, 0, 0, 10);
    c.anchor = GridBagConstraints.NORTHWEST;
    panel.add(btnPanel, c);
  }

  private ActionListener getAddBtnListener() {
    return new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        trainingImageSelector.addTrainingImage();
        controller.repaint();
      }
    };
  }

  private ActionListener getDelBtnListener() {
    return new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        trainingImageSelector.removeTrainingImage();
        controller.repaint();
      }
    };
  }

  private ActionListener getProcessBtnListener() {
    return new ActionListener() {

      public void actionPerformed(ActionEvent e) {

        if (!trainingImageSelector.isTrainingImagesValid()) {
          JOptionPane.showMessageDialog(null, "Check training images!");
        } else {
          try {
            ArrayList<TrainingImageSpec> images = trainingImageSelector.getTrainingImages();
            HashMap<Character, ArrayList<TrainingImage>> hashMap = TrainingImageUtils.getTrainingImageHashMap(images);
            
            File trainingImagesFile = new File(controller.getSettings().getTrainingImages());
            trainingImagesFile.delete();
            ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(trainingImagesFile));
            outputStream.writeObject(hashMap);
            outputStream.close();

            trainingImages = hashMap;
            JOptionPane.showMessageDialog(null, "Done!");
          } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Error!");
            System.out.println(ex);
            ex.printStackTrace();
          }
        }
      }
    };
  }
}